package spring.cloud.tasks.api.controller;

import org.springframework.web.bind.annotation.*;
import spring.cloud.tasks.api.service.AlarmStatisticsService;
import spring.cloud.tasks.api.utils.Response;

import javax.annotation.Resource;

/**
 * 作业的告警统计
 *
 * @author hebelala
 */
@RequestMapping("/console/namespaces/{namespace:.+}")
public class JobAlarmStatisticsController {

    @Resource
    private AlarmStatisticsService alarmStatisticsService;

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/jobs/{jobName}/isAbnormal")
    public Response isAbnormalJob(@PathVariable String namespace, @PathVariable String jobName)
            throws RuntimeException {
        return Response.success(alarmStatisticsService.isAbnormalJob(namespace, jobName));
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/jobs/{jobName}/isUnableFailover")
    public Response isUnableFailoverJob(@PathVariable String namespace, @PathVariable String jobName)
            throws RuntimeException {
        return Response.success(alarmStatisticsService.isUnableFailoverJob(namespace, jobName));
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    @GetMapping(value = "/jobs/{jobName}/isTimeout4Alarm")
    public Response isTimeout4AlarmJob(@PathVariable String namespace, @PathVariable String jobName)
            throws RuntimeException {
        return Response.success(alarmStatisticsService.isTimeout4AlarmJob(namespace, jobName));
    }


    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})

    @PostMapping(value = "/setAbnormalJobMonitorStatusToRead")
    public Response setAbnormalJobMonitorStatusToRead(@RequestParam String uuid,
                                                      @PathVariable String namespace) throws RuntimeException {
        //assertIsPermitted(PermissionKeys.alarmCenterSetAbnormalJobRead, namespace);
        alarmStatisticsService.setAbnormalJobMonitorStatusToRead(uuid);
        return new Response();
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})

    @PostMapping(value = "/setTimeout4AlarmJobMonitorStatusToRead")
    public Response setTimeout4AlarmJobMonitorStatusToRead(@RequestParam String uuid,
                                                           @PathVariable String namespace) throws RuntimeException {
        //assertIsPermitted(PermissionKeys.alarmCenterSetTimeout4AlarmJobRead, namespace);
        alarmStatisticsService.setTimeout4AlarmJobMonitorStatusToRead(uuid);
        return new Response();
    }


}
